/**
 * @author whz
 */

var flag = false;
// @Bind #dataTree.onContextMenu
!function (self, arg) {

    var param = {"position": {"left": arg.event.clientX, "top": arg.event.clientY}}
    view.get("#menu1").show(param);
}

// @Bind #menuAdd.onClick
!function (treeGrid) {
    var currentNode = view.get("#dataTree").get("currentNode");
    if (!currentNode) {
        throw new dorado.Exception("请先点中一个树节点！");
    }
    var currentEntity = currentNode.get("data");
    var newEntity = currentEntity.get("childCategories").insert({
        parentId: currentEntity.toJSON().id
    });
    view.get("#dataTree").set("currentEntity", newEntity)

    view.get("#dialogEdit").show();
    flag = true;
}

// @Bind #menuAddBorther.onClick
!function () {
    var currentNode = view.get("#dataTree").get("currentNode");
    if (!currentNode) {
        throw new dorado.Exception("请先点中一个树节点！");
    }
    var currentEntity = currentNode.get("data");
    var newEntity = currentEntity.createBrother({
        parentId: currentEntity.toJSON().parentId
    });
    view.get("#dataTree").set("currentEntity", newEntity)
    view.get("#dialogEdit").show();
    flag = true;
}

//@Bind #btnConfirm.onClick
!function () {
    var dialog = view.get("#dialogEdit");
    view.get("#updateAction").execute(function () {
        dialog.hide();
    })
}
//@Bind #btnCancel.onClick
!function () {
    view.get("#dialogEdit").hide();
    if (flag) {
        flag = false;
        view.get("#dsCategories").getData("!currentCategory").remove();

    }
}

// @Bind #dialogEdit.onClose
!function () {
    if (flag) {
        flag = false;
        //view.get("#dsCategory.data:#").remove()
        view.get("#dsCategories").getData("!currentCategory").remove();
    }
}

//@Bind #menuDel.onClick

!function () {
    var entity = view.get("#dsCategories").getData("!currentCategory");

    if (entity) {
        dorado.MessageBox.confirm("确认要删除记录吗？", function () {
            entity.remove();
            view.get("#updateAction").execute()
        })

    }
}


